package com.afd.ozblip.activity;

import com.afd.ozblip.service.MyLocationService;
import com.afd.ozblip.service.MyService;
import com.afd.ozblip.util.PhoneUtil;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WelcomeActivitybak extends BaseActivity {

	/****************** 视图变量 **************************/
	private LinearLayout settingLayout;
	private TextView tipTv1;
	private MyLocationService myService;
	private MyReceiver receiver;

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			myService = ((MyLocationService.MyBinder) service).getService();
		}

		public void onServiceDisconnected(ComponentName arg0) {

		}
	};

	private Handler myHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				Location lc = myService.mLocationClient.getLastLocation();
				System.out.println(lc);
				commonLocation = lc;
				Intent indexIntent = new Intent(WelcomeActivitybak.this,
						IndexActivity.class);
				startActivity(indexIntent);
				finish();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		receiver = new MyReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("android.intent.action.MY_RECEIVER");
		// 注册
		registerReceiver(receiver, filter);
	}

	@Override
	protected void onStart() {
		super.onStart();
		// 判断是否有网络
		if (PhoneUtil.hasNetwork(this)) {
			// 开起定位服务
			Intent intent = new Intent(this, MyLocationService.class);
			startService(intent);
			Intent i = new Intent();
			i.setClass(this, MyLocationService.class);
			this.bindService(i, mConnection, BIND_AUTO_CREATE);

		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 不要忘了这一步
		unregisterReceiver(receiver);
//		// 暂停定位服务
//		this.unbindService(mConnection);
//		Intent i = new Intent();
//		i.setClass(this, MyService.class);
//		stopService(i);

	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.tipTv1 = (TextView) findViewById(R.id.tv_welcome_tip_text1);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener welcomeClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.tv_welcome_tip_text1:

					break;

				default:
					break;
				}
			}
		};
		this.tipTv1.setOnClickListener(welcomeClick);
	}

	/**
	 * 广播接收器
	 * 
	 * @author user
	 * 
	 */
	private class MyReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			boolean result = bundle.getBoolean("isConnect", false);
			if (result) {
				myHandler.sendEmptyMessage(1);
			}
		}
	}
}